﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business_Layer;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
            // Neu khong co Tham So 'CategoryID'
        if (!IsPostBack)
        {
            if (String.IsNullOrEmpty(Request["CategoryID"]))
            {
                this.SearchProducts();
            }
            else
            {
                this.FillProducts();
            }
        }
 
    }

    void SearchProducts()
    {
        String Search = "%" + Request["txtSearch"] + "%";
        String sql = "SELECT * FROM Products WHERE ProductName LIKE @Search";
        DataTable Products = Database.GetData(sql, "@Search", Search);

        DataList1.DataSource = Products;
        DataList1.DataBind();
    }

    void FillProducts()
    {
        String CategoryID = Request["CategoryID"];
        //String sql = "SELECT * FROM Products WHERE CategoryID=@CategoryID";
        //DataTable Products = Database.GetData(sql, "@CategoryID", CategoryID);
        if (CategoryID == "All")
        {
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 6; // số items hiển thị trên một trang.
            CollectionPager1.DataSource = Category_BUS.getProduct().DefaultView;
            CollectionPager1.BindToControl = DataList1;
            DataList1.DataSource = CollectionPager1.DataSourcePaged;
            DataList1.DataBind();
        }
        else
        {
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 6; // số items hiển thị trên một trang.
            CollectionPager1.DataSource = Product_BUS.getProductsByCatID(int.Parse(CategoryID)).DefaultView;
            CollectionPager1.BindToControl = DataList1;
            DataList1.DataSource = CollectionPager1.DataSourcePaged;
            DataList1.DataBind();
        }
    }

    protected void btnImage_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton btn = (ImageButton)sender;
        int ProductID = int.Parse(btn.CommandArgument);

        Response.Redirect("ProductDetail.aspx?ProductID=" + ProductID);
    }

    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        ShoppingCart Cart = (ShoppingCart)Session["ShoppingCart"];

        LinkButton btn = (LinkButton)sender;
        int ProductID = int.Parse(btn.CommandArgument);
        Cart.Add(ProductID, 1);

        ASP.masterpage_master master = (ASP.masterpage_master)this.Master;

      
        master.RefreshShoppingCartInfo();
        this.FillProducts();
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
